package com.spzx.product;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spzx.common.security.annotation.EnableCustomConfig;
import com.spzx.common.security.annotation.EnableRyFeignClients;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.mapper.ProductSkuMapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
public class SpzxProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpzxProductApplication.class, args);
    }

    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    private ProductSkuMapper productSkuMapper;


    //当项目启动后 加载所有的skuId存到bitmap中
    @PostConstruct //jdk原生注解，标注的方法会在 当前类的对象初始化后调用
    public void init() {
        //查询所有的sku的id集合
        List<Long> skuIdList = productSkuMapper.selectList(Wrappers.lambdaQuery(ProductSku.class)
                        .select(ProductSku::getId))
                .stream().map(ProductSku::getId)
                .collect(Collectors.toList());

        //将skuId存入到bitmap中
        skuIdList.forEach(skuId -> {
            stringRedisTemplate.opsForValue()
                    //使用skuId当作 偏移量，将值设置为1  表示offset这个位置的skuId存在
                    .setBit("spzx:product:skuIds", skuId, true);
            //参数1：bitmap数组的键，参数2：数组的偏移量(数组的下标) 参数3：存入到偏移量位置的值 只能是0/1
        });
    }


/*
    //商品功能
    //初始化布隆过滤器
    @Override
    public void run(String... args) throws Exception {
        //创建布隆过滤器
        RBloomFilter<Object> bloomFilter =
                redissonClient.getBloomFilter("sku:bloom:filter");
        //设置布隆过滤器 //设置数据规模 误判率
        bloomFilter.tryInit(100000,0.01);

        //查询mysql里面商品skuId
        List<ProductSku> productSkuList = productSkuMapper.selectList(null);
        productSkuList.forEach(item -> {
            bloomFilter.add(item.getId());
        });
    }*/

}